<?php

namespace App\Http\Controllers\Strator;

use App\Http\Controllers\Controller;
use Illuminate\Database\Eloquent\Model;
use App\Models\Strator\StratorCheck;
use App\Models\Strator\StratorProduct;
use App\Models\Strator\StratorCheckDetail;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\Strator\StratorSupplier;
use App\Models\Strator\StratorCustomer;

class StratorCheckController extends Controller
{
    protected Request $request;
    protected Model $model;

    public function __construct(Request $request,StratorCheck $strator)
    {
        $this->request=$request;
        $this->model = $strator;
    }

    /**
     * Display a listing of the resource.
     */
    public function getchecktime()
    {
        $list=$this->model->select('check_time','order_sn','id')->orderBy('id', 'asc')
        ->get();
        return $this->success($list);
    }
    public function index()
    {
        $list=$this->model
        ->modelSearch($this->request,$this->model)
        ->whereIn('order_sn', $this->request->order_sn)
        ->orderByDesc('id')
        ->get();
        $this->saveOperationLog('查询盘存列表');
        return $this->success($list);
    }
    public function getItem()
    {
        $list=$this->model
        ->where('id', $this->request->id)
        ->orderByDesc('id')
        ->get();
        $this->saveOperationLog('查询盘存列表');
        return $this->success($list->get());
    }

    /**
     * Show the form for creating a new resource.干什么？
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store()
    {
        $data=$this->request->only($this->storeOnly());
        $data['order_sn'] = $this->getSn();
        $this->buildDetail($this->request,$data['order_sn']);
        $this->saveOperationLog('新增盘存单');
        return $this->success('新建成功');
    }
    protected function buildDetail($request,$order_sn){
        // 按数据生成出盘存明细
        $contents = json_decode($request->contents);
        $data = [];
        for ($i=0;$i<count($contents);$i++){
            $data[$i]= [
                'order_sn'=> $order_sn,
                'created_at'=>now(),
                'pro_id'=>$contents[$i]->pro_id??null,
                'remark'=>$contents[$i]->remark??'',
                'inventory'=>$contents[$i]->inventory??0,
                'check_num'=>$contents[$i]->check_num??0,
                'check_time'=>$contents[$i]->check_time??null,
            ];
        }

        if(count($data)>0){
            DB::table('strator_checks')->insert($data);
        }
    }
    //设置订单号
    private function getSn(){
        $car = StratorCheck::query()
            ->select('id')
            ->where('id','>',0)
            ->orderByDesc('id')
            ->first();
        if($car){
            $num = $car->id+1;
        }else{
            $num = 1;
        }
        if ($num<10) {
            $num = '000'.$num;
        } elseif ($num<100) {
            $num = '00'.$num;
        } elseif ($num<1000) {
            $num = '0'.$num;
        }
        return 'pcd'.date('Ymd',time()).$num;
    }
    // 
    public function storeOnly():array{
        return [
            'order_sn',
            'pro_id',
            'remark',
            'inventory',
            'check_num',
            'check_time',
        ];
    }

    /**
     * Display the specified resource.
     */
    public function show()
    {
//        return $this->success($this->request->user());
        $model=$this->model->findOrFail($this->request->id);
        return $this->success($model);
    }

    /**
     * Update the specified resource in storage.
     */
    public function update()
    {
        $model=$this->model->findOrFail($this->request->id);
        $origin=$model->toArray();
        $data=$this->request->except($this->updateExcept());
        $model->fill($data);
        $model->save();
        $this->saveOperationLog('修改盘存单',$origin);
        return $this->successMsg();
    }
    //
    public function updateExcept():array
    {
        return [
            'id',
        ];
    }
    /**
     * Remove the specified resource from storage.
     */
    public function destroy()
    {
        $model=$this->model->findOrFail($this->request->id);
        $model->delete();
        $this->saveOperationLog('删除盘存单');
        return $this->successMsg('删除成功');
    }
}
